package com.zb.service.impl;

import com.zb.dao.LoginMapper;
import com.zb.model.TUser;
import com.zb.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoginMapper lm;
	
	public TUser loginInspectService(String phone,String passwd) {
		TUser user=lm.loginInspect(phone);
		if (user==null) {
			return null;
		} else {
			if(user.getPasswd().equals(passwd)) {
				return user;
			}else
				return null;
		}
		}

	public boolean registerService(String phone,String passwd,String username,String email) {
		
		TUser user=new TUser();
		try {
			user.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
			user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		user.setPasswd(passwd);
		user.setPhone(phone);
		user.setUsername(username);
		user.setEmail(email);
		user.setUserId(UUID.randomUUID().toString());
		if(lm.register(user)) {
			return true;
		}
		System.out.println("Service����ʵ���"+user.toString());
		return false;
	}


	public void resetpswService(String phone, String sms) {
		TUser user=new TUser();
		try {
			user.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		user.setPasswd(sms);
		user.setPhone(phone);
		lm.resetpsw(user);
	}

	public void renameService(String userid,String phone,String username,String email) {
		TUser user=new TUser();
		user.setEmail(email);
		user.setUserId(userid);
		user.setPhone(phone);
		user.setUsername(username);
		lm.rename(user);
	}

	@Override
	public boolean yanzhengoldService(String userid, String password) {
		TUser user=new TUser();
		user.setUserId(userid);
		//user.setPasswd(password);
		TUser selected = lm.yanzhengold(user);
		if(selected.getPasswd().equals(password)) {
			return true;
		}else {
			return false;
		}
		
	}

	@Override
	public boolean repwService(String userid, String newpassword) {
		TUser user=new TUser();
		user.setUserId(userid);
		user.setPasswd(newpassword);
		lm.repw(user);
		return true;
		
	}
	
}
